package com.javasm.handler;

import com.alibaba.fastjson.JSON;
import com.javasm.domain.ResponseResult;
import com.javasm.utlis.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author：MoDebing
 * @Version：1.0
 * @Date：2022-09-02-10:53
 * @Description: 认证过程出现异常的处理机制
 */
//@Component
public class AuthenticationEntryHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(),"用户认证失败，请重新登录");
        String json = JSON.toJSONString(result);
        // 处理异常
        WebUtils.renderString(response,json);


    }
}
